//-----------------------------------------------------------------------
// <copyright file="ProductPresenter.cs" company="Simnova LLC">
//     Copyright (c) Simnova LLC. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Simnova.Collaborate.Presenter
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    public class ProductPresenter
    {
        private readonly View.IProduct productView;
        private int companyId;

        public ProductPresenter(View.IProduct productView, int companyId)
        {
           // System.Data.Design.TypedDataSetSchemaImporterExtension
            this.companyId = companyId;
            this.productView = productView;
            this.productView.Name = String.Empty;
            this.productView.Description = String.Empty;
            this.LoadProducts();
        }

        public void LoadProducts()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            this.productView.ProductList = collaborateClient.GetProductsByCompany(this.companyId);
            collaborateClient.Close();
        }

        public void LoadProduct(int productId)
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            AppServer.Product product = collaborateClient.GetProduct(productId);
            collaborateClient.Close(); 
            
            if (product != null)
            {
                this.productView.ProductId = product.ProductId;
                this.productView.Name = product.Name;
                this.productView.Description = product.Description;
            }
            else
            {
                this.productView.ProductId = 0;
                this.productView.Name = "";
                this.productView.Description = "";
            }
        }

        public void Save()
        {
            AppServer.Product product = new AppServer.Product();
            product.ProductId = this.productView.ProductId;
            product.CompanyId = this.companyId;
            product.Name = this.productView.Name;
            product.Description = this.productView.Description;

            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            collaborateClient.SaveProduct(product);
            collaborateClient.Close();
            this.LoadProducts();
        }
    }
}
